package com.example.myivideo

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Debug
import android.os.Handler
import android.os.Looper
import android.os.Trace
import android.util.Log
import android.view.View
import androidx.fragment.app.Fragment
import androidx.viewpager2.widget.ViewPager2.OnPageChangeCallback
import com.example.common.utils.ColdStartUtils
import com.example.home.ui.HomeFragment
import com.example.mvicore.ui.BaseMVIActivity
import com.example.myivideo.adapter.HomeViewPagerAdapter
import com.example.myivideo.databinding.ActivityMainBinding
import com.google.android.material.badge.BadgeDrawable
import com.google.android.material.navigation.NavigationBarView
import com.tencent.bugly.crashreport.CrashReport

class MainActivity : BaseMVIActivity() {

    private lateinit var binding: ActivityMainBinding

    private val handler : Handler = Handler(Looper.getMainLooper())

    private var isInit:Boolean = false

    override fun initView() {
        super.initView()

        handler.looper.queue.addIdleHandler {
            if (!isInit){
                //bugly初始化 不建议异步初始化 可以考虑延时初始化
                CrashReport.initCrashReport(getApplicationContext(), "528d63dbd7", BuildConfig.DEBUG)
                isInit=true
            }
            true
        }

        binding.bnvHomeBottom.setLabelVisibilityMode(NavigationBarView.LABEL_VISIBILITY_LABELED)
        val item:Int = binding.bnvHomeBottom.getMenu().getItem(3).itemId
        val badge:BadgeDrawable = binding.bnvHomeBottom.getOrCreateBadge(item)
        badge.badgeGravity = BadgeDrawable.TOP_END
        badge.isVisible = true
        badge.number = 90

        val fragments:List<Fragment> = getFragments()
        val adapter = HomeViewPagerAdapter(this,fragments)
        binding.vpHome.adapter = adapter
    }

    override fun initEvent() {
        super.initEvent()
        binding.bnvHomeBottom.setOnNavigationItemSelectedListener { item ->
            binding.vpHome.setCurrentItem(item.order)
            true
        }

        binding.vpHome.registerOnPageChangeCallback(object : OnPageChangeCallback(){
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                binding.bnvHomeBottom.menu.getItem(position).setChecked(true)
            }
        })
    }

    override fun generateModel() {
        TODO("Not yet implemented")
    }

    override fun generateLayoutView(): View {
        binding = ActivityMainBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun onWindowFocusChanged(hasFocus: Boolean) {
        super.onWindowFocusChanged(hasFocus)
        //手动埋点位置 结束冷启动耗时
        val duration = ColdStartUtils.endRecodeTime()
        Log.w("cpa", "cold start duration:${duration}")
        Debug.stopMethodTracing()
        Trace.endSection()
    }

    /**
     * 创建业务组件Fragment
     */
    private fun getFragments():List<Fragment>{
        val list : MutableList<Fragment> = ArrayList()
        list.add(HomeFragment())
        list.add(HomeFragment())
        list.add(HomeFragment())
        list.add(HomeFragment())
        return list
    }
}


